/* eslint-disable vue/no-dupe-keys */
<template>
  <div class="goods-container">

    <bread first="商品管理"
           second="商品列表"></bread>

    <!-- 卡片 -->
    <el-card class="box-card">
      <div class="searchBox">
        <el-input placeholder="请输入内容"
                  v-model="paramsObj.query"
                  class="input-with-select">
          <el-button @click.native="getUsers"
                     slot="append"
                     icon="el-icon-search"></el-button>

        </el-input>
        <el-button type="primary"
                   @click="$router.push('/add')">添加商品</el-button>
      </div>

      <!-- 表格 -->
      <el-table border
                :data="tableData"
                style="width: 100%">
        <el-table-column type="index"
                         label="#"
                         width="80">
        </el-table-column>
        <el-table-column prop="goods_name"
                         label="商品名称"
                         width="720">
        </el-table-column>
        <el-table-column prop="goods_price"
                         label="商品价格(元)"
                         width="150">
        </el-table-column>
        <el-table-column prop="goods_number"
                         label="商品重量"
                         width="110">
        </el-table-column>
        <el-table-column prop="add_time"
                         label="创建时间"
                         width="200">
        </el-table-column>
        <el-table-column prop=""
                         label="操作"
                         width="150">
          <el-button type="primary"
                     size="small"
                     icon="el-icon-edit"></el-button>
          <!-- 删除按钮 -->
          <el-button type="danger"
                     size="small"
                     @click="open"
                     icon="el-icon-delete"></el-button>
        </el-table-column>
      </el-table>
      <!-- 表格 -->
      <!-- 分页组件 -->
      <el-pagination style="margin-top:20px"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="paramsObj.pagenum"
                     :page-sizes="[5, 10, 15, 20]"
                     :page-size="paramsObj.pagesize"
                     background
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </el-card>
     
  </div>
</template>
           
<script>
import { goodsApi } from '../api'

import moment from 'moment'
export default {
  name: '',
  data () {
    return {
      //分页
      currentPage1: 5,
      currentPage2: 5,
      currentPage3: 5,
      currentPage4: 4,

      //点击搜索按钮进行=查找
      paramsObj: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      searchVal: '',
      tableData: [],

    }
  },
  created () {
    this.getUsers()
  },
  methods: {
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`);
      this.paramsObj.pagesize = val
      this.getUsers() //重新根据页面获取数据
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`);
      this.paramsObj.pagenum = val
      this.getUsers() //重新根据页面获取数据
    },

    async getUsers () {
      const res = await goodsApi(this.paramsObj)
      console.log(res, 666);
      // 转换时间格式	
      res.data.data.goods.forEach(item => item.add_time = moment(item.add_time).format('YYYY-MM-DD hh:mm:ss'))
      console.log(res.data.data.goods);
      this.tableData = res.data.data.goods;
      // 获取总条数
      this.total = res.data.data.total

    },


    // 删除按钮
    open () {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  },
}
</script>
<style lang='less'  scoped>
.goods-container {
  .box-card {
    .searchBox {
      margin: 15px 0 20px;
      display: flex;
      .el-input {
        width: 400px;
        margin-right: 20px;
      }
    }

    .el-pagination {
      text-align: left;
    }
  }
}
</style>
